import _extends from "@babel/runtime/helpers/esm/extends";
import {createNamespace} from '../utils';
import {padZero} from '../utils/format/string';
import {range} from '../utils/format/number';
import {sharedProps, TimePickerMixin} from './shared';

var _createNamespace = createNamespace('time-picker'),
    createComponent = _createNamespace[0];

export default createComponent({
    mixins: [TimePickerMixin],
    props: _extends({}, sharedProps, {
        minHour: {
            type: Number,
            default: 0
        },
        maxHour: {
            type: Number,
            default: 23
        },
        minMinute: {
            type: Number,
            default: 0
        },
        maxMinute: {
            type: Number,
            default: 59
        }
    }),
    computed: {
        ranges: function ranges() {
            return [{
                type: 'hour',
                range: [this.minHour, this.maxHour]
            }, {
                type: 'minute',
                range: [this.minMinute, this.maxMinute]
            }];
        }
    },
    watch: {
        filter: 'updateInnerValue',
        minHour: 'updateInnerValue',
        maxHour: 'updateInnerValue',
        minMinute: 'updateInnerValue',
        maxMinute: 'updateInnerValue',
        value: function value(val) {
            val = this.formatValue(val);

            if (val !== this.innerValue) {
                this.innerValue = val;
                this.updateColumnValue(val);
            }
        }
    },
    methods: {
        formatValue: function formatValue(value) {
            if (!value) {
                value = padZero(this.minHour) + ":" + padZero(this.minMinute);
            }

            var _value$split = value.split(':'),
                hour = _value$split[0],
                minute = _value$split[1];

            hour = padZero(range(hour, this.minHour, this.maxHour));
            minute = padZero(range(minute, this.minMinute, this.maxMinute));
            return hour + ":" + minute;
        },
        updateInnerValue: function updateInnerValue() {
            var _this$getPicker$getIn = this.getPicker().getIndexes(),
                hourIndex = _this$getPicker$getIn[0],
                minuteIndex = _this$getPicker$getIn[1];

            var hour = this.originColumns[0].values[hourIndex] || this.originColumns[0].values[0];
            var minute = this.originColumns[1].values[minuteIndex] || this.originColumns[1].values[0];
            var value = hour + ":" + minute;
            this.innerValue = this.formatValue(value);
        },
        onChange: function onChange(picker) {
            var _this = this;

            this.updateInnerValue();
            this.$nextTick(function () {
                _this.$nextTick(function () {
                    _this.$emit('change', picker);
                });
            });
        },
        updateColumnValue: function updateColumnValue() {
            var _this2 = this;

            var formatter = this.formatter;
            var pair = this.innerValue.split(':');
            var values = [formatter('hour', pair[0]), formatter('minute', pair[1])];
            this.$nextTick(function () {
                _this2.getPicker().setValues(values);
            });
        }
    }
});